﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Category
{
    public partial class Edit : AdminPage
    {
        private CategoryInfo model = null;
        private Category curCategory = new Category();
        private string clsno = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            clsno = Request.QueryString["clsno"] == null ? "" : Request.QueryString["clsno"].Trim();
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            string ClassNo = this.hidClassNo.Value.Trim();
            string ClassName = this.txtClassName.Text.Trim();
            string ClassList = "";                                            //栏目包含列表
            string ClassPre = this.DdlMenu.SelectedValue.Trim();              //上一级目录
            string preClassList = "";
            string curClassList = "";
            int clsListLen = 1;                                               //栏目深度

            if (ClassName != "")
            {
                if (ClassPre == "0")
                {
                    ClassList = ClassNo + ",";
                    preClassList = "0";
                    curClassList = "1";
                }
                else
                {
                    DataTable dt = curCategory.GetListRow(ClassPre);
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        preClassList = dr["clslist"].ToString().Trim();
                        ClassList = dr["clslist"].ToString().Trim() + ClassNo + ",";
                        clsListLen = Convert.ToInt32(dr["clslistlen"]) + 1;
                        curClassList = curCategory.GetClslistByNo(ClassNo);
                    }
                }
                if (preClassList!="" && !preClassList.StartsWith(curClassList))
                {
                    model = new CategoryInfo();
                    model.clsno = ClassNo;
                    model.clsname = ClassName;
                    model.clslist = ClassList;
                    model.clsparentno = ClassPre;
                    model.clslistlen = clsListLen;
                    if (curCategory.Update(model, false))
                    {
                        Response.Redirect("Admin.aspx");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('栏目保存操作失败！')", true);
                    }
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('栏目保存操作失败！不能将该目录设置到该目录的下级目录！')", true);
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('栏目保存操作失败！栏目名或生成目录为空！')", true);
            }
        }


        #region 数据绑定
        private void BindData()
        {

            this.btnEdit.Attributes.Add("onclick", "return ChkInput()");
            DataTable dt = curCategory.GetOrderList();

            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("设置为根类别", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                int clsListLen = Convert.ToInt32(dr["clslistlen"]);
                string ClassNo = dr["clsno"].ToString().Trim();
                string ClassName = dr["clsname"].ToString().Trim();
                if (clsListLen == 1)
                {
                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassNo));

                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = PlugNT.Common.Utils.StringOfChar(clsListLen - 1, "　") + ClassName;
                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassNo));
                }

                if (ClassNo == clsno)
                {
                    hidClassNo.Value = ClassNo;
                    model = curCategory.GetCategoryInfo(ClassNo);
                    DdlMenu.SelectedValue = curCategory.GetParentClsno(ClassNo);
                    txtClassName.Text = model.clsname;
                }
            }
            this.DdlMenu.Attributes.Add("onchange", "SwapText();");
        }
        #endregion


    }
}
